﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Vector2 velocity;
    private Vector3 direction;

    private bool hasMoved;

    private void Update()
    {
        if (System.Math.Abs(velocity.x) <= 0) {
            hasMoved = false;
        } else if (System.Math.Abs(velocity.x) > 0 && !hasMoved) {
            hasMoved = true;
            MoveByDirection();
        }
        velocity = new Vector2(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"));
    }


    private void MoveByDirection() {
        if (velocity.x < 0)
        {
            if (velocity.y > 0)
            {
                direction = new Vector3(-0.5f, 0.5f);
            }
            else if (velocity.y < 0)
            {
                direction = new Vector3(-0.5f, -0.5f);
            }
            else
            {
                direction = new Vector3(-1f, 0f);
            }
        }
        else if (velocity.x > 0)
        {
            if (velocity.y > 0)
            {
                direction = new Vector3(0.5f, 0.5f);
            }
            else if (velocity.y < 0)
            {
                direction = new Vector3(0.5f, -0.5f);
            }
            else
            {
                direction = new Vector3(1f, 0f);
            }
        }
        else {
            if (velocity.y > 0)
            {
                direction = new Vector3(0, 0.5f);
            }
            else if (velocity.y < 0)
            {
                direction = new Vector3(0, -0.5f);
            }
            //else
            //{
            //    direction = new Vector3(0, 1f);
            //}
        }
        transform.position += direction;
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        
    }

}
